﻿<template>
  <div class="l-rblock l-form-viewer learun-el-form-left" style="padding: 8px">
    <el-form
      :model="formData"
      :rules="rules"
      size="mini"
      labelPosition="left"
      labelWidth="100px"
      ref="form"
    >
      <div class="l-auto-window only-tabs">
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane
            class="l-tabs-container"
            :label="$t('主表信息')"
            name="tab0"
          >
            <el-row :gutter="16">
              <div class="l-rblock">
                <el-col :span="18">
                  <el-form-item
                    :label="$t('供应商编号')"
                    prop="case_erp_supplier_f_Number"
                  >
                    <l-code
                      code="erp_supplier_num"
                      v-if="formData.case_erp_supplier_f_IsSysNum == '0'"
                      size="mini"
                      v-model="formData.case_erp_supplier_f_Number"
                    />
                    <el-input
                      v-else
                      size="mini"
                      v-model="formData.case_erp_supplier_f_Number"
                      maxlength="15"
                      :disabled="isEdit"
                      :placeholder="$t('请输入16位数字编码')"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item
                    prop="case_erp_supplier_f_IsSysNum"
                    labelWidth="10px"
                  >
                    <l-checkbox
                      :options="case_erp_supplier_f_IsSysNumOptions"
                      :disabled="isEdit"
                      v-model="formData.case_erp_supplier_f_IsSysNum"
                      @change="checkChange"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item
                    :label="$t('供应商名称')"
                    prop="case_erp_supplier_f_Name"
                  >
                    <el-input
                      v-model="formData.case_erp_supplier_f_Name"
                      :placeholder="$t('请输入')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item
                    :label="$t('负责人')"
                    prop="case_erp_supplier_f_Person"
                  >
                    <el-input
                      v-model="formData.case_erp_supplier_f_Person"
                      :placeholder="$t('请输入')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item
                    :label="$t('手机号')"
                    prop="case_erp_supplier_f_Phone"
                  >
                    <el-input
                      v-model="formData.case_erp_supplier_f_Phone"
                      :placeholder="$t('请输入')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item
                    :label="$t('经营范围')"
                    prop="case_erp_supplier_f_Scope"
                  >
                    <el-input
                      v-model="formData.case_erp_supplier_f_Scope"
                      :placeholder="$t('请输入')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item
                    :label="$t('供应商类型')"
                    prop="case_erp_supplier_f_Type"
                  >
                    <l-select
                      :multiple="true"
                      :options="case_erp_supplier_f_TypeOptions"
                      v-model="formData.case_erp_supplier_f_Type"
                    />
                  </el-form-item>
                </el-col>
              </div>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-form>
  </div>
</template>
<script>
const api = window.$api.erpcase.supplier;

export default {
  data() {
    return {
      moduleCode: "ERP_Suppliers",
      activeName: "tab0",
      formData: {
        case_erp_supplier_f_State: 0,
        case_erp_supplier_f_Number: "", // 供应商编号
        case_erp_supplier_f_IsSysNum: "0", // 唯一标识(GUID)
        case_erp_supplier_f_Name: "", // 供应商名称
        case_erp_supplier_f_Person: "", // 负责人
        case_erp_supplier_f_Phone: "", // 手机号
        case_erp_supplier_f_Scope: "", // 经营范围
        case_erp_supplier_f_Type: "", // 供应商类型
      },

      rules: {
        case_erp_supplier_f_Number: [
          { required: true, message: "请输入供应商编号" },
        ],
        case_erp_supplier_f_Name: [
          { required: true, message: "请输入供应商名称" },
          {
            validator: this.lr_existDbFiled,
            fieldName: "f_Name",
            keyValue: () => {
              return this.formData.case_erp_supplier_f_Id;
            },
            tableName: "case_erp_supplier",
            keyName: "f_Id",
          },
        ],
        case_erp_supplier_f_Phone: [
          {
            pattern:
              /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
            message: "请输入电话/手机号",
          },
        ],
        case_erp_supplier_f_Scope: [
          { required: true, message: "请输入经营范围" },
        ],
        case_erp_supplier_f_Type: [
          { required: true, message: "请选择供应商类型" },
        ],
      },
      isEdit: false,
    };
  },
  computed: {
    case_erp_supplier_f_IsSysNumOptions() {
      return [{ value: "0", label: "用系统编码" }];
    },
    case_erp_supplier_f_TypeOptions() {
      return [
        { value: "其他", label: "其他" },
        { value: "半成品", label: "半成品" },
        { label: "成品", value: "成品" },
        { label: "原料", value: "原料" },
      ];
    },
  },
  methods: {
    // 选择使用系统编码
    checkChange() {
      this.formData.case_erp_supplier_f_Number = "";
      if (parseInt(this.formData.case_erp_supplier_f_IsSysNum) != 0) {
        this.rules.case_erp_supplier_f_Number.push({
          validator: this.lr_existDbFiled,
          fieldName: "f_Number",
          keyValue: () => {
            return this.formData.case_erp_supplier_f_Id;
          },
          tableName: "case_erp_supplier",
          keyName: "f_Id",
        });
      } else {
        // this.rules.case_erp_apply_f_Number.splice(1, 1);
      }
    },

    // 重置表单
    resetForm() {
      this.$formClear(this.$refs.form);
      this.activeName = "tab0";
      this.isEdit = false;
    },
    // 校验表单
    async validateForm() {
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false;
      }

      return true;
    },
    // 设置表单数据
    setForm(data) {
      this.$setFormData(this.formData, data, "case_erp_supplier");
      this.isEdit = true;
    },
    // 获取表单数据
    getForm() {
      return this.$getFormData(this.formData, "case_erp_supplier");
    },

    async loadFormData({ keyValue /*,params,node*/ }) {
      //  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
      const data = await this.$awaitWraper(api.get(keyValue));
      if (data) {
        this.setForm(data);
        return true;
      } else {
        return false;
      }
    },

    async saveForm({ keyValue, isEdit /*,code,node*/ }) {
      // isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
      const postData = this.getForm();
      if (isEdit) {
        // 编辑
        return await this.$awaitWraper(api.update(keyValue, postData));
      } else {
        // 新增
        postData.f_Id = keyValue;

        return await this.$awaitWraper(api.add(postData));
      }
    },
  },
};
</script>